/*
 * @Author: vera_min vera_min@163.com
 * @Date: 2024-12-17 21:51:45
 * @LastEditors: vera_min vera_min@163.com
 * @LastEditTime: 2025-01-13 01:03:06
 * @FilePath: /mail-project/src/main.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { createApp, nextTick } from 'vue'
import App from "./App.vue";
import router from '/@/router';
import pinia from '/@/stores/index';
import { installGlobalDraggableDirectives } from '/@/directive/draggable';
import './tauri';
import TwoDialog from "./components/Common/TwoDialog.vue";

// 应用初始化工具
import { handleVisibilityChange, initializeApp } from '/@/utils/appInit'


import '/@/theme/index.scss';

const app = createApp(App);

installGlobalDraggableDirectives(app);

app.directive('clickOut', {
  beforeMount(el, binding) {
    el.clickOutsideEvent = (e:any) => {
      if(!(el === e.target || el.contains(e.target))) {
        binding.value(e); // 执行绑定的方法
      }
    }
    document.addEventListener("click", el.clickOutsideEvent);
  },
  
  unmounted(el) {
    document.removeEventListener("click", el.clickOutsideEvent);
  }
});

app.component('TwoDialog', TwoDialog);

app.use(pinia).use(router).mount('#app');

// 应用挂载完成后执行初始化
nextTick(() => {
  // 执行应用初始化（包括IM自动重连）
  initializeApp()

  // 监听页面可见性变化，用于处理页面切换时的IM重连
  document.addEventListener('visibilitychange', handleVisibilityChange)
})